﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Appsters
{
    public partial class yearView : Form
    {
        private int yearCounter = DateTime.UtcNow.Year;
        private OleDbConnection dbConn;

        public yearView()
        {
            InitializeComponent();
            DateTime dt = DateTime.UtcNow.Date;
            month.Text = dt.ToString("m");
            year.Text = dt.Year.ToString();
            
            int yearCounter = DateTime.UtcNow.Year;

            OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder();
            csb["provider"] = "Microsoft.ACE.OLEDB.12.0";
            csb["data source"] = Application.StartupPath + @"\Jotter.accdb";
            string connStr = csb.ConnectionString;
            dbConn = new OleDbConnection(connStr);

            titleBox.ReadOnly = true;
            dateBox.ReadOnly = true;
            alarmBox.ReadOnly = true;
            statBox.ReadOnly = true;
            titleBox.BackColor = SystemColors.Window;
            dateBox.BackColor = SystemColors.Window;
            alarmBox.BackColor = SystemColors.Window;
            statBox.BackColor = SystemColors.Window;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            year.Text = ((yearCounter) - 1).ToString();
            yearCounter = int.Parse(year.Text);
            refreshList();
        }

        private void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            year.Text = ((yearCounter) + 1).ToString();
            yearCounter = int.Parse(year.Text);
            refreshList();
        }

        private void janButton_Click(object sender, EventArgs e)
        {
            month.Text = "January " + yearCounter;
            monthInNum = "01";
            refreshList();
        }

        private void febButton_Click(object sender, EventArgs e)
        {
            month.Text = "February " + yearCounter;
            monthInNum = "02";
            refreshList();
        }

        private void marchButton_Click(object sender, EventArgs e)
        {
            month.Text = "March " + yearCounter;
            monthInNum = "03";
            refreshList();
        }

        private void aprButton_Click(object sender, EventArgs e)
        {
            month.Text = "April " + yearCounter;
            monthInNum = "04";
            refreshList();
        }

        private void mayButton_Click(object sender, EventArgs e)
        {
            month.Text = "May " + yearCounter;
            monthInNum = "05";
            refreshList();
        }

        private void juneButton_Click(object sender, EventArgs e)
        {
            month.Text = "June " + yearCounter;
            monthInNum = "06";
            refreshList();
        }

        private void julButton_Click(object sender, EventArgs e)
        {
            month.Text = "July " + yearCounter;
            monthInNum = "07";
            refreshList();
        }

        private void augButton_Click(object sender, EventArgs e)
        {
            month.Text = "August " + yearCounter;
            monthInNum = "08";
            refreshList();
        }

        private void sepButton_Click(object sender, EventArgs e)
        {
            month.Text = "September " + yearCounter;
            monthInNum = "09";
            refreshList();
        }

        private void octButton_Click(object sender, EventArgs e)
        {
            month.Text = "October " + yearCounter;
            monthInNum = "10";
            refreshList();
        }

        private void novButton_Click(object sender, EventArgs e)
        {
            month.Text = "November " + yearCounter;
            monthInNum = "11";
            refreshList();
        }

        private void decButton_Click(object sender, EventArgs e)
        {
            month.Text = "December " + yearCounter;
            monthInNum = "12";
            refreshList();
        }

        private void year_Click(object sender, EventArgs e)
        {

        }

        private void backButton_Click(object sender, EventArgs e)
        {
            backButton.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);           
            planner plan = new planner();
            plan.Show();
            this.Hide();
        }

        private void refreshList()
        {
            titleBox.Clear();
            dateBox.Clear();
            alarmBox.Clear();

            string yr = year.Text;

            dbConn.Open();

            string qryStr = @"SELECT title, theDate, alarm FROM Task";

            OleDbCommand cmd = new OleDbCommand(qryStr, dbConn);
            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                string defT = ""; //default for titleBox
                string defD = ""; //default for dateBox
                string defA = ""; //default for alarmBox

                string t = (string)reader.GetValue(0);
                string d = (string)reader.GetValue(1); // this is the date in the format yyyy-MM-dd
                string a = (string)reader.GetValue(2);

                string currentDay = d.Substring(8, 2);
                string currentYear = d.Substring(0, 4);

                if (monthInNum == d.Substring(5, 2) && yr == currentYear)
                {
                    titleBox.AppendText(defT + t + "\r\n");
                    dateBox.AppendText(defD + currentDay + "\r\n");
                    alarmBox.AppendText(defA + a + "\r\n");
                }
            }
            dbConn.Close();
        }

        private void yearView_Load(object sender, EventArgs e)
        {
            refreshList();
        }
    }
}
